package View;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;






import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.ArrayList;
import Controller.*;
import Model.*;
public class ViewGame extends JFrame implements  Observer {
	
	JLabel lbl;
	JFrame frame, controlFrame;
	Timer timer;
	//JPanel panel;
	JPanel contentPanel, gamePanel, gameOverPanel;
	ControllerGame controller;
	ArenaModel model;
	boolean gameOver;
	boolean jugadorGana;
	private Graphics2D g2d = null;
	private KeyListenerAdapter keyListener;
	
	public ViewGame (ControllerGame controller, ArenaModel model){
		
		this.controller = controller;
		this.model = model;
		model.registerObserver(this);
		
		iniciarVista();
	}
	/**
	 * Estable los parametros del jframe
	 */
	private void iniciarVista(){
		gameOver = false;
		jugadorGana = false;
		// setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(new Dimension(800, 600));
		setBackground(Color.BLACK);
		setVisible(true);
	}
	/**
	 * Inicia el jpanel.
	 */
	public void createView() {
	
		contentPanel = new JPanel();
		gamePanel = new JPanel();
      
        //add(this);
		keyListener = new KeyListenerAdapter();
        gamePanel.addKeyListener(keyListener);
        gamePanel.setFocusable(true);
        contentPanel.setBackground(Color.BLACK);
        gamePanel.setDoubleBuffered(true);
        //getContentPane().add(gamePanel);
        contentPanel.add(gamePanel);
        addWindowListener(new WindowAdapter() {
        	 public void windowClosing(WindowEvent e) {
        		 System.exit(0);
        		 }
        });
        
       
        // agregamos al frame el panel contenedor
        getContentPane().add(contentPanel);
      
       
	}
	public void gameOver(){
		gameOver = true;
		gamePanel.removeKeyListener(keyListener);
		KeyAdapter keyAdapter = new KeyAdapter()
		{
		  public void keyPressed(KeyEvent e)
		  {
			  controller.irMenu();
			  System.out.println("llendo al menu");

		  }
		};
		gamePanel.addKeyListener(keyAdapter);
		
        
    
        
	}
	public void jugadorGana(){
		gameOver = true;
		jugadorGana = true;
		gameOver();
		
	}
	public void createControls(){}
	
	
	
	// es mi observer
	public void updateObserver(){
		repaint();
	}
	
	public void paint(Graphics g){
		
		if(!gameOver){
			
			super.paint(g);
			dibujarEnemigos(g);
			dibujarAliado(g);
			dibujarMisiles(g);
			dibujarHUD(g);
	        Toolkit.getDefaultToolkit().sync();
	        //g.dispose();
	       
		}
		
		else{
		
			super.paint(g);
			dibujarGameOver(g);
			if(jugadorGana)
				dibujarJugadorGana(g);
			Toolkit.getDefaultToolkit().sync();
	        g.dispose();
			
			
		}
	}
	private void dibujarGameOver(Graphics g){
		
		g.setFont(new Font("sansserif", Font.BOLD, 20));
		g.setColor(Color.yellow);
		g.drawString("GAME OVER ",300,300);
		g.drawString("Presione cualquier tecla para continuar",300,320);
		
	}
	private void dibujarJugadorGana(Graphics g){
		
		g.setFont(new Font("sansserif", Font.BOLD, 20));
		g.setColor(Color.yellow);
		g.drawString("Ha ganado. Puntos: "+ model.getAliado().getPuntos(),300,260);
		
	}
	private void dibujarHUD(Graphics g)
	{
		//g2d = (Graphics2D)g;
		g.setFont(new Font("sansserif", Font.BOLD, 20));
		g.setColor(Color.yellow);
		g.drawString("Puntos: "+ model.getAliado().getPuntos(), 600, 500);
		
		g.setFont(new Font("sansserif", Font.BOLD, 16));
		
		g.drawString("Vidas "+ model.getAliado().getVidas()+" - Escudo "+ model.getAliado().getEscudoAsString(), 600, 530);
	}
	
	private void dibujarAliado(Graphics g)
	{
		
		NaveAliada aliado = this.model.getAliado();
		
		g.drawImage(aliado.getImage(), aliado.getX(), aliado.getY(), this);
	}
	
	private void dibujarMisiles(Graphics g){
		ArrayList<Misil> ms = new ArrayList<Misil>();
		
		ms = model.getMisilesActualizados();
        for (int i = 0; i < ms.size(); i++ ) {
            Misil m = (Misil) ms.get(i);
            g.drawImage(m.getImage(), m.getX(), m.getY(), this);
        }
	}
	
	private void dibujarEnemigos(Graphics g){
		
		ArrayList<NaveModelInterface> enemies = this.model.getEnemigos();
		
		NaveModelInterface alien ;
        for(int i = 0; i < enemies.size(); i ++)
        {	
			alien = (NaveModelInterface) enemies.get(i);
			g.drawImage(alien.getImage(), alien.getX(), alien.getY(), this);
 	
        	
        }
	}
	
	private class KeyListenerAdapter extends KeyAdapter {

		public void keyReleased(KeyEvent e) {
					
				controller.keyReleased(e);
		}

		public void keyPressed(KeyEvent e) {
			
				controller.keyPressed(e);
			
			
		}
	}
	
}
